//
//  VideoDetailViewController.m
//  News
//
//  Created by xalo on 16/3/1.
//  Copyright © 2016年 News. All rights reserved.
//

#import "VideoDetailViewController.h"
#import "VideoModel.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "UIViewController+Notice.h"
#import <UIImageView+WebCache.h>
#import "ShowViewController.h"
#import "CollectViewController.h"

#import "UMSocial.h"

@interface VideoDetailViewController ()
- (IBAction)backToLastView:(id)sender;


@property (strong, nonatomic) IBOutlet UILabel *dateLabel;

@property (strong, nonatomic) IBOutlet UILabel *sourceLabel;

@property (strong, nonatomic) IBOutlet UILabel *longTitleLabel;

@property (strong, nonatomic) IBOutlet UIImageView *backView;

@property (strong, nonatomic) IBOutlet UILabel *smallTitleLabel;

@property (strong, nonatomic) IBOutlet UILabel *introLabel;

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic,strong) AVPlayerViewController *avPlayer;

@property (strong, nonatomic) IBOutlet UIView *scrollBackView;


- (IBAction)playAction:(UIButton *)sender;



@property (strong, nonatomic) IBOutlet NSLayoutConstraint *ScrollViewHeight;


@property (strong, nonatomic) IBOutlet UIButton *shareButton;


- (IBAction)collectAndShared:(UIBarButtonItem *)sender;

@property (nonatomic,assign) BOOL isSelected;

@property (nonatomic,strong) UIImageView *clearView;



@end

@implementation VideoDetailViewController



-(AVPlayerViewController *)avPlayer
{
    if (!_avPlayer) {
        _avPlayer = [[AVPlayerViewController alloc]init];
    }

    return _avPlayer;
}

- (IBAction)playAction:(UIButton *)sender {
    
    
    
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:self.videoModel.url]];
    
    self.avPlayer.player = [[AVPlayer alloc]init];
    
    [self.avPlayer.player replaceCurrentItemWithPlayerItem:item];
    self.avPlayer.view.frame = CGRectMake(0, 0, sender.frame.size.width, sender.frame.size.height);
    
    [sender addSubview:self.avPlayer.view];
  
    
    
    [self.avPlayer.player play];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    self.isSelected = NO;
    
    
    [self setViewWithModel];
    
    
    [self changeBackgroung];
   
  
    
    
}

//截屏方fa
-(UIImageView *)screenShot {
    
    
    
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(KScreenWidth, KScreenHeight), YES, 1);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef imageRef = viewImage.CGImage;
    
    CGRect rect = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
    
    self.clearView = [[UIImageView alloc]initWithImage:sendImage];
    self.clearView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    
    return self.clearView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)updateViewConstraints
{

    [super updateViewConstraints];
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        
        self.ScrollViewHeight.constant = (CGRectGetMaxY(self.shareButton.frame) + self.shareButton.frame.size.height + 64) - self.view.frame.size.height;
    });
    
    
}



-(void)setViewWithModel
{
    self.dateLabel.text = [self changeDateToStringWithModel:self.videoModel];
    self.sourceLabel.text = self.videoModel.source;
   self.longTitleLabel.text= self.videoModel.long_title;
//    self.backView.image = self.videoModel.image;
    [self.backView.image imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    [self.backView sd_setImageWithURL:[NSURL URLWithString:self.videoModel.kpic] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    
    self.smallTitleLabel.text = self.videoModel.long_title;
    self.introLabel.text = [NSString stringWithFormat:@"        %@",self.videoModel.intro];
    
    //
    
}

-(void)viewDidDisappear:(BOOL)animated
{
    [self.avPlayer.view removeFromSuperview];
    self.avPlayer = nil;
    

}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backToLastView:(id)sender {
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


-(NSString *)changeDateToStringWithModel:(VideoModel *)model {

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.pubDate];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSString *string = [formatter stringFromDate:date];

    return string;
    
}


//通知
-(void)changeBackgroung
{

    extern BOOL isNightMode;
    if (isNightMode) {
        
        self.view.backgroundColor = [UIColor darkGrayColor];
        
    }else {
    
        self.view.backgroundColor = [UIColor whiteColor];
      
    }

}


- (IBAction)collectAndShared:(UIBarButtonItem *)sender {
    
    
     [self screenShot];
    
    
    CollectViewController *collect = [[UIStoryboard storyboardWithName:@"Collect" bundle:nil] instantiateViewControllerWithIdentifier:@"collect"];

    collect.videoModel = self.videoModel;
    collect.pictureView = self.clearView;
    collect.pictureLink = self.videoModel.url;
    collect.titShare = self.videoModel.title;
    
    
    [self.view addSubview:collect.view];
    [self addChildViewController:collect];

    
}


//快捷分享
- (IBAction)shareAction:(UIButton *)sender {
    
    if (sender.tag == 111) {
        

        UMSocialUrlResource *source = [[UMSocialUrlResource alloc] initWithSnsResourceType:(UMSocialUrlResourceTypeImage) url:self.videoModel.url];
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQzone] content:[NSString stringWithFormat:@"%@",self.videoModel.title] image:nil location:nil urlResource:source presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
            }
        }];
        

        
    }else if (sender.tag == 112){
        
        NSLog(@"微信");
        UMSocialUrlResource *source = [[UMSocialUrlResource alloc] initWithSnsResourceType:(UMSocialUrlResourceTypeImage) url:self.videoModel.url];

        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:[NSString stringWithFormat:@"%@",self.videoModel.title] image:nil location:nil urlResource:source presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
            }
        }];
        
        
    }else if (sender.tag == 113){
        
      
        UMSocialUrlResource *source = [[UMSocialUrlResource alloc] initWithSnsResourceType:(UMSocialUrlResourceTypeImage) url:self.videoModel.url];

        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToTencent] content:[NSString stringWithFormat:@"%@",self.videoModel.title]  image:nil location:nil urlResource:source presentedController:self completion:^(UMSocialResponseEntity *response) {
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                
                
                
                
            }
        }];
        
        
    }
    
    
}









@end
